// pages/accounts/addstudent/addstudent.js
const screenservice = require("../../../apiservice/screen/screenservice");
const screencustomerservice = require("../../../apiservice/screen/screencustomerservice");
const app = getApp();
var util = require('../../../utils/util.js');
const jsondataConfig = require("../jsondata");

Page({
 
	/**
	 * 页面的初始数据
	 */
	data: {
		screenId: "",
		templateid: "",
		new_screenteamId: "",
		screenSchoolItem: null,
		isOpen: false,
		opName: "更多联系方式",
		desease: jsondataConfig.desease,
		deseaseIndex: 0,
		genderIndex: 0,
		lead: {
			new_patientsource: 7,
		},
		maxDate: new Date().getTime(),
		startDate: util.frmatDate(new Date()),
		endDate: util.frmatDate(new Date()),
		birthdateShow: false,
		currentDate: new Date().getTime(),
		minDate: new Date(1919, 1, 1).getTime(),
    IsShowtip:false,
		samenameqty:'',
		screenOwnerId:""
	},

	onbirthConfirm: function(e) {
		var time = e.detail;
		var birthday = util.frmatDate(new Date(time));
		var age = util.getAgeBybirthDay(birthday);
		this.setData({
			['lead.new_birthdate']: util.frmatDate(new Date(time)),
			['lead.new_age']: age,
			birthdateShow: false
		})
	},
	onbirthCancel: function(e) {
		this.setData({
			birthdateShow: false
		})
	},

	onSelectBirthdate: function(e) {
		this.setData({
			birthdateShow: true,
		})
	},

	//班级
	onSelectClass: function(e) {
		var screenSchoolItem = this.data.screenSchoolItem;
		var region = this.data.region;
		wx.navigateTo({
			url: `../../class/list/list?type=choose&schoolId=${screenSchoolItem.schoolId}&stage=${screenSchoolItem.stage}`		
		})
	},

	//病种
	changeDesease: function(e) {
		var index = e.detail.value;
		this.setData({
			deseaseIndex: index,
		});
		if (index != 0) {
			this.setData({
				['lead.new_disease']: this.data.desease[index].id
			});
		}
	},

	//姓名
	onNameChange: function(e) {
		this.setData({
			['lead.lastname']: e.detail
		});
	},

  onNameBlur:function(e){
    let scid = this.data.screenId;
    var that = this;
    screencustomerservice.GetSameNameQuantity(scid, e.detail.value).then(function (res) {
      console.log(res.data);
      that.setData({
        IsShowtip: true,
        samenameqty: "+" + res.data
      })
    });

  },

	//手号码
	onMobilephoneChange: function(e) {
		this.setData({
			['lead.address1_telephone1']: e.detail,
			['lead.new_phone']: e.detail
		});
	},

	//家长手机
	onMobilephone2Change: function(e) {
		this.setData({
			['lead.address1_telephone3']: e.detail
		});
	},

	//家庭固话
	onTelephone2Change: function(e) {
		this.setData({
			['lead.telephone3']: e.detail
		});
	},

	//年龄
	onAgeChange: function(e) {
		this.setData({
			['lead.new_age']: e.detail
		});
	},

	//性别
	onGenderChange: function(e) {
		this.setData({
			genderIndex: e.detail.index
		});
	},

	//家庭住址
	onHomeAddrChange: function(e) {
		this.setData({
			['lead.address1_name']: e.detail
		});
	},

	//家长QQ
	onQQChange: function(e) {
		this.setData({
			['lead.new_qq']: e.detail
		});
	},

	//家长微信
	onWechatChange: function(e) {
		this.setData({
			['lead.new_wechat']: e.detail
		});
	},
	//学号
	onStudentIdChange: function(e) {
		var result = 'lead.new_studentid'
		this.setData({
			[result]: e.detail
		})
	},

	onSubmit: function() {
		//字段验证
		if (!this.checkPhoneAndIDCardAndName(this.data.lead))
			return;
		else
			this.SaveLeadAccount();
	},

	SaveLeadAccount: function() {

		var scobj = {};
		var that = this;
		wx.showLoading({
			title: '正在添加...',
			mask: true
		})

		var lead = this.data.lead;
		var screenSchoolItem = this.data.screenSchoolItem;
		lead.new_school = screenSchoolItem.schoolId;
		lead.new_class = screenSchoolItem.classId;
		lead.new_gender = this.data.genderIndex + 1;
		lead.new_stage = screenSchoolItem.stage;
		lead.userid= app.globalData.userInfo.userid;
		lead.ownerid = this.data.screenOwnerId;
		scobj.lead = lead;
		scobj.customer = {
			new_scid: this.data.screenId,
			new_screenteamid: this.data.new_screenteamId
		}

		console.log(scobj);

		screenservice.CreateSQXXCJ(scobj).then(function(success) {
			console.log(success);
			that.setData({
				lead: {
					new_patientsource: 7,
				},
				deseaseIndex: 0,
        IsShowtip:false
			});
			const eventChannel = that.getOpenerEventChannel();
			eventChannel.emit('refreshEvent', {
				data: screenSchoolItem.classId
			});

			wx.hideLoading();
			wx.showToast({
				title: '添加成功',
				icon: 'success',
				duration: 2000
			});
		}).catch(function(ex) {
			console.log(ex);
			wx.hideLoading();
			wx.showToast({
				title: '添加失败',
				icon: 'none',
				duration: 2000
			});
		})

	},

	openOrclose: function(e) {
		var flag = this.data.isOpen;
		if (!flag) {
			this.setData({
				isOpen: true,
				opName: '收起'
			})
		} else {
			this.setData({
				isOpen: false,
				opName: '更多联系方式'
			})
		}
	},


	checkPhoneAndIDCardAndName: function(obj) {
		var mobile = obj.new_phone;
    var mobile2 = obj.address1_telephone3;
		var idcard = obj.new_citizenid;
		var qq = obj.new_qq;
		var name = obj.lastname;
		var age = obj.new_age;
		var mobilereg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		var idreg =
			/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
		var qqreg = /[1-9][0-9]{4,}/
		if (!name || name == '') {
			wx.showToast({
				title: '请填写名字',
				icon: 'none',
				duration: 1500
			})
			return false;
		}
		if (age != '') {
			if (age > 150) {
				wx.showToast({
					title: '请填写正确的年龄',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}
		if (mobile) {
			if (!mobilereg.test(mobile)) {
				wx.showToast({
					title: '请填写正确的手机号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (idcard) {
			if (!idreg.test(idcard)) {
				wx.showToast({
					title: '请填写正确的身份证号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (mobile2) {
			if (!mobilereg.test(mobile2)) {
				wx.showToast({
					title: '请填写正确的家长手机号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (qq) {
			if (!qqreg.test(qq)) {
				wx.showToast({
					title: '请填写正确的qq号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}
		return true;
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			screenId: options.new_screenId,
			templateid: options.templateid,
			new_screenteamId: options.new_screenteamId,
			screenSchoolItem: options,
			screenOwnerId:options.ownerid
		})
		console.log(this.data.screenSchoolItem);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
